package script.node;

import cn.hutool.core.util.HexUtil;
import com.gee.spring.cloud.aerospace.simulate.core.domain.TelemetryNodeDto;
import com.gee.spring.cloud.aerospace.simulate.core.satellite.telemetry.TmFrameGenerator;
import com.gee.spring.cloud.aerospace.simulate.core.util.StaticRedisUtil;
import com.gee.spring.cloud.aerospace.simulate.core.util.TelemetryNodeUtil;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

import static cn.hutool.core.text.StrPool.COLON;
import static com.gee.spring.cloud.aerospace.simulate.core.constant.CommonConstant.VC_COUNT_KEY;

/**
 * 虚拟信道帧计数
 * @author gepengjun
 * @since 2024/7/4 14:21
 */
public class VcCount {

    static byte[] eval(TmFrameGenerator tmFrameGenerator, TelemetryNodeDto currentNode,
                       List<TelemetryNodeDto> frameNodeList, Map<String, String> scriptParamMap){
        String path = scriptParamMap.get("path");
        long min = Long.parseLong(scriptParamMap.get("min"));
        long max = Long.parseLong(scriptParamMap.get("max"));
        TelemetryNodeDto node = TelemetryNodeUtil.getNode(path, frameNodeList);
        StringJoiner keyJoiner = new StringJoiner(COLON);
        keyJoiner
                .add(VC_COUNT_KEY)
                .add(tmFrameGenerator.getSatelliteTelemetryDto().getSateId().toString())
                .add(HexUtil.encodeHexStr(node.getBytes()));
        long vcCount = StaticRedisUtil.loopIncrease(keyJoiner.toString(), min, max);
        return HexUtil.decodeHex(HexUtil.toHex(vcCount));
    }
}
